package com.example

/**
@author whd
@date 2023/11/10 11:34:15
 */

object NumberUtil {
  fun numberToChinese(number: Int): String {
    val chineseUnit = arrayOf("", "十", "百", "千")
    val chineseDigits = arrayOf("", "一", "二", "三", "四", "五", "六", "七", "八", "九")

    var result = ""
    var n = number
    var digitPosition = 0
    var lastNonZero = false

    while (n > 0) {
      val digit = n % 10
      if (digit != 0) {
        result = chineseDigits[digit] + chineseUnit[digitPosition] + result
        lastNonZero = true
      } else {
        if (lastNonZero) {
          result = "零$result"
          lastNonZero = false
        }
      }

      digitPosition++
      n /= 10
    }
    if (number<20&&number>=10){result = result.substring(1,result.length)}
    return if (result.isEmpty()) "零" else result
  }
}


